﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Systemet.SideViews
{
    public partial class SideAnstalld : UserControl
    {
        private Domain.PersonalController pC;
        private Views.Personalkort empMainView;
        private List<Personalregister> employees;
        private IEnumerable<Personalavdelning> departments;

        public SideAnstalld()
        {
            InitializeComponent();
            AccessController.Instance.SidMeny = this;
        }

        private void SideAnstalld_Load(object sender, EventArgs e)
        {
            FlowController.Instance.ShowSpecifikMenu(FlowController.Instance.MainTabView[0]);
            empMainView = (Views.Personalkort)AccessController.Instance.ReturnMainView();
            RetrieveFilterOptions();
        }

        private void RetrieveFilterOptions()
        {
            pC = new Domain.PersonalController();
            employees = pC.RetrieveEmployees();
            departments = pC.retrieveDepartment();
            fillComboBox(employees);
        }
        private void fillComboBox(List<Personalregister> employees)
        {
            List<Personalregister> Inlagda = new List<Personalregister>();
            Inlagda.Clear();
            foreach (Personalregister p in employees)
            {
                var basicCheck = Inlagda.Find(iP => iP.personnummer == p.personnummer);
                if (basicCheck == null)
                {
                    cmbPersonalNamn.Items.Add(p.Fnamn + " " + p.Enamn);
                    Inlagda.Add(p);
                }
            }
            AutoCompleteStringCollection persNr = new AutoCompleteStringCollection();
            foreach (Personalregister p in Inlagda)
            {
                if (!persNr.Contains(p.personnummer))
                {
                    persNr.Add(p.personnummer);
                }
            }

            tbPersNr.AutoCompleteCustomSource = persNr;
            tbPersNr.AutoCompleteMode = AutoCompleteMode.Suggest;
            tbPersNr.AutoCompleteSource = AutoCompleteSource.CustomSource;
        }

        private void OnTextSearchChange(object sender, EventArgs e)
        {
            string avdelning = cmbPersonalAvd.Text;
            string Pnamn = cmbPersonalNamn.Text;
            string persNr = tbPersNr.Text;

            if (cmbPersonalAvd.Text == "Alla")
            {
                avdelning = "";
            }
            if (tbPersNr.Text != "" && tbPersNr.Focused == true)
            {
                cmbPersonalNamn.Text = "";
            }
            if (cmbPersonalNamn.Text != "" && cmbPersonalNamn.Focused == true)
            {

                persNr = "";
                tbPersNr.Text = "";
            }

            
            empMainView = (Views.Personalkort)AccessController.Instance.MainView;
            empMainView.updateDgv(avdelning, Pnamn, persNr);

        }

        private void OnSearchIndexChange(object sender, EventArgs e)
        {
            ClearFilterOptions();
            OnTextSearchChange(sender, e);

            FilterSearchOptions();

        }

        private void FilterSearchOptions()
        {
            string avd = cmbPersonalAvd.Text;
            if (cmbPersonalAvd.Text == "Alla")
            {
                avd = "";
            }
            cmbPersonalNamn.Items.Clear();
            List<Personalregister> Inlagda = new List<Personalregister>();
            Inlagda.Clear();
            foreach (Personalregister p in employees)
            {
                foreach (Personalavdelning pA in departments)
                {
                    if (pA.avdelningsnamn.TrimEnd().Contains(avd))
                    {

                        if (p.personnummer == pA.personnummer)
                        {

                            var basicCheck = Inlagda.Find(iP => iP.personnummer == p.personnummer);
                            if (basicCheck == null)
                            {
                                cmbPersonalNamn.Items.Add(p.Fnamn + " " + p.Enamn);
                                Inlagda.Add(p);
                            }
                        }
                    }
                }
            }

        }

        //Cleara sökkriterier
        private void ClearFilterOptions()
        {
            cmbPersonalNamn.Text = "";
            tbPersNr.Text = "";
        }


    }
}
